package com.aisi.manager.controller;

import com.aisi.commonUtil.AuthContextUtil;
import com.aisi.manager.service.SysMenuService;
import com.aisi.manager.service.SysUserService;
import com.aisi.manager.service.ValidateCodeService;
import com.aisi.model.dto.system.LoginDto;
import com.aisi.model.entity.system.SysUser;
import com.aisi.model.vo.common.Result;
import com.aisi.model.vo.common.ResultCodeEnum;
import com.aisi.model.vo.system.LoginVo;
import com.aisi.model.vo.system.SysMenuVo;
import com.aisi.model.vo.system.ValidateCodeVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: shenjianZ
 * @Date: 2024/2/16 19:38
 * @Description:
 */
@Tag(name = "用户接口")
@RestController
@RequestMapping("/admin/system/index")
public class IndexController {

    @Resource
    private SysUserService sysUserService;

    @Resource
    private ValidateCodeService validateCodeService;

    @Resource
    private SysMenuService sysMenuService;

    @Operation(summary = "登录接口")
    @PostMapping("/login")
    public Result<LoginVo> login(@RequestBody LoginDto loginDto) {
        LoginVo loginVo = sysUserService.login(loginDto);
        return Result.build(loginVo, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "生成验证码")
    @GetMapping("/generateValidateCode")
    public Result<ValidateCodeVo> generateValidateCode() {
        ValidateCodeVo validateCodeVo = validateCodeService.generateValidateCode();
        return Result.build(validateCodeVo, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "获取用户信息")
    @GetMapping("/getUserInfo")
    public Result<LoginVo> getUserInfo() {
        SysUser sysUser = AuthContextUtil.getAuthContext(); //
        return Result.build(sysUser, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "用户退出登录")
    @GetMapping("/logout")
    public Result<Void> logout(@RequestHeader(name = "token") String token) {
        sysUserService.logout(token);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @GetMapping("/menus")
    public Result menus() {
        List<SysMenuVo> sysMenuVoList = sysMenuService.findUserMenuList();
        return Result.build(sysMenuVoList,ResultCodeEnum.SUCCESS);
    }


}
